package com.example.demo.social.QQ.api.impl;

import com.example.demo.social.QQ.api.QQ;
import com.example.demo.social.QQ.api.QQUserInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.social.oauth2.AbstractOAuth2ApiBinding;
import org.springframework.social.oauth2.TokenStrategy;

/**
 * @author xkx
 * @date 2019/1/7
 */
@Slf4j
public class QQImpl extends AbstractOAuth2ApiBinding implements QQ {

    // first:获取authorization code, https://graph.qq.com/oauth2.0/authorize
    // second:获取authorization token, https://graph.qq.com/oauth2.0/token


  //http://wiki.connect.qq.com/openapi%E8%B0%83%E7%94%A8%E8%AF%B4%E6%98%8E_oauth2-0
  //get openId according to token
  //openId:OpenID是此网站上或应用中唯一对应用户身份的标识，
  // 网站或应用可将此ID进行存储，便于用户下次登录时辨识其身份，或将其与用户在网站上或应用中的原有账号进行绑定。
  private static final String QQ_URL_GET_OPENID = "https://graph.qq.com/oauth2.0/me?access_token=%s";
  //http://wiki.connect.qq.com/get_user_info(access_token由父类提供)
  private static final String QQ_URL_GET_USER_INFO = "https://graph.qq.com/user/get_user_info?oauth_consumer_key=%s&openid=%s";
  /**
   * appId 配置文件读取
   */
  private String appId;
  /**
   * openId 请求QQ_URL_GET_OPENID返回
   */
  private String openId;
  /**
   * 工具类
   */
  private ObjectMapper objectMapper = new ObjectMapper();

  /**
   * 构造方法获取openId
   */
  public QQImpl(String accessToken, String appId) {
    //access_token作为查询参数来携带。
    super(accessToken, TokenStrategy.ACCESS_TOKEN_PARAMETER);

    this.appId = appId;

    String url = String.format(QQ_URL_GET_OPENID, accessToken);
    String result = getRestTemplate().getForObject(url, String.class);

    log.info("【QQImpl】 QQ_URL_GET_OPENID={} result={}", QQ_URL_GET_OPENID, result);

    this.openId = StringUtils.substringBetween(result, "\"openid\":\"", "\"}");
  }

  @Override
  public QQUserInfo getUserInfo() {
    String url = String.format(QQ_URL_GET_USER_INFO, appId, openId);
    String result = getRestTemplate().getForObject(url, String.class);

    log.info("[QQImpl] QQ_URL_GET_USER_INFO={} result={}", QQ_URL_GET_USER_INFO, result);

    QQUserInfo userInfo = null;
    try {
      userInfo = objectMapper.readValue(result, QQUserInfo.class);
      userInfo.setOpenId(openId);
      return userInfo;
    } catch (Exception e) {
      throw new RuntimeException("获取用户信息失败", e);
    }
  }
}
